﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Lapas.Common.Forms;
using WorkDAL;
using Lapas.Common;
using WorkDAL.Users;
using LookupControl;


namespace Users
{
    public partial class UsersChangePassWordForm : EditForm
    {
        public UsersChangePassWordForm()
        {
            InitializeComponent();
            base.OkButton = btnOK;
            base.CloseButton = btnClose;
        }

        public tbUser users = new tbUser();


        private void UsersChangePassWordForm_Load(object sender, EventArgs e)
        {
            txtUserName.Enabled = false;
            txtUserFullName.Enabled = false;
            txtOldPassWord.Enabled = false;

            txtUserName.Text = users.UserName;
            txtUserFullName.Text = users.UserFullName;
            txtOldPassWord.Text = users.Password;
        }


        protected override void BindData()
        {
            if (base.FormAction != EditFormAction.Change)
            {
                txtUserName.Text = users.UserName;
                txtUserFullName.Text = users.UserFullName;
                txtOldPassWord.Text = users.Password;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtOldPassWord.Text.Trim() != users.Password)
                {
                    er.SetError(txtOldPassWord, "Mật khẩu cũ không đúng");
                    return;
                }

                if (txtNewPassWord.Text.Trim() != txtConfirmPassWord.Text.Trim())
                {
                    er.SetError(txtConfirmPassWord, "Mật khẩu xác thực không đúng");
                    return;
                }

                users.Password = Helper.Encrypt(txtNewPassWord.Text.Trim());
                UserDataAccess.Instance().UpdateUser(users);

                DisplayMessage.ShowConfirm("Đã thực hiện xong!");
                this.Close();
            }
            catch (Exception ex)
            {
                DisplayMessage.ShowError(ex.Message);
                return;
            }
        }

        private void txtUserName_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void txtUserName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F5 || e.KeyCode == Keys.Enter || e.KeyCode == Keys.Tab)
            {
                LookupForm lookupForm = new LookupForm("UserName, UserFullName", "Ng sử dụng, Họ và tên", "tbUsers", "UserName", txtUserName.Text.Trim(), "Ng sử dụng");
                lookupForm.ShowDialog();
                txtUserName.Text = lookupForm.ValueLookup;
            }
        }
    }
}
